package com.icas.exception;

import com.icas.entity.resultentity.Result;
import com.icas.enums.MsgResultEnum;
import com.icas.enums.ShiroEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: tocchen
 * @Date: 2022/4/22 12:43
 * @Description 全局异常的捕获处理
 **/
@RestControllerAdvice
@Log4j2
public class MyExceptionHandler {


    @ExceptionHandler(value = AuthorizationException.class)
    public Result<?> authorizationException(AuthorizationException e){
        log.warn(e.getMessage());
        return Result.error(ShiroEnum.authorization);
    }

    @ExceptionHandler(value = AuthenticationException.class)
    public Result<?> authenticationException(AuthenticationException e){
        log.warn(e.getMessage());
        return Result.error(401,e.getMessage());
    }

    @ExceptionHandler(value = JsonFormatException .class)
    public Result<?> jsonFormatException(JsonFormatException e){
        log.warn(e.getMessage());
        return Result.error(MsgResultEnum.DATA_FORMAT_ERROR);
    }

    @ExceptionHandler(value = UpDateException .class)
    public Result<?> upDateException(UpDateException e){
        log.warn(e.getMessage());
        return Result.error(MsgResultEnum.UPDATE_ERROR);
    }

    @ExceptionHandler(value = LoginException .class)
    public Result<?> loginException(LoginException e){
        log.warn(e.getMessage());
        return Result.error(MsgResultEnum.Account_ERROR);
    }

    @ExceptionHandler(value = QueryNotFoundException.class)
    public Result<?> queryNotFoundException(QueryNotFoundException e){
        log.warn(e.getMessage());
        return Result.error(MsgResultEnum.QUERY_FAILED);
    }
}
